# -*- coding:utf-8 -*-
import wx
import requests as nq
import json
import getAccessToken as iot 

nq.packages.urllib3.disable_warnings()
netAdress = 'develop.api.ct10649.com'
port = '8743'
cert = ('./client.crt', './client.key')
appID = 'Y8W_1ZxbNak8_HSQgRkHIbt2_jka'
secret = 'YCCuEKEGY3tbh7LCpoKiEjTJtHAa'
deviceID = gateWayId = 'eaec8bb4-51f4-4362-966a-ac16f6e27deb'
accessToken = ''

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='水质监测系统V0.1', size=(400, 300),name='frame',style=541072384)
        self.启动窗口 = wx.Panel(self)
        self.启动窗口.SetOwnBackgroundColour((255, 255, 255, 255))
        self.Centre()
        self.按钮1 = wx.Button(self.启动窗口,size=(100, 32),pos=(146, 203),label='已排除异物',name='button')
        按钮1_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',28)
        self.按钮1.SetFont(按钮1_字体)
        self.按钮1.Bind(wx.EVT_BUTTON,self.按钮1_按钮被单击)
        self.标签2 = wx.StaticText(self.启动窗口,size=(60, 32),pos=(20, 60),label='温度：',name='staticText',style=17)
        标签2_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',28)
        self.标签2.SetFont(标签2_字体)
        self.标签1 = wx.StaticText(self.启动窗口,size=(60, 32),pos=(20, 20),label='浊度：',name='staticText',style=17)
        标签1_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.标签1.SetFont(标签1_字体)
        self.标签3 = wx.StaticText(self.启动窗口,size=(60, 32),pos=(20, 100),label='湿度：',name='staticText',style=17)
        标签3_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.标签3.SetFont(标签3_字体)
        self.标签4 = wx.StaticText(self.启动窗口,size=(60, 32),pos=(20, 140),label='PH值：',name='staticText',style=17)
        标签4_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.标签4.SetFont(标签4_字体)
        self.标签5 = wx.StaticText(self.启动窗口,size=(80, 32),pos=(180, 20),label='水面高度：',name='staticText',style=17)
        标签5_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.标签5.SetFont(标签5_字体)
        self.时钟1 = wx.Timer(self)
        self.时钟1.Start(5000)
        self.Bind(wx.EVT_TIMER,self.时钟1_周期事件,self.时钟1)
        self.标签6 = wx.StaticText(self.启动窗口,size=(80, 32),pos=(180, 60),label='浊度状态：',name='staticText',style=17)
        标签6_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.标签6.SetFont(标签6_字体)
        self.标签7 = wx.StaticText(self.启动窗口,size=(80, 32),pos=(180, 100),label='P H 状态：',name='staticText',style=17)
        标签7_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.标签7.SetFont(标签7_字体)
        self.标签8 = wx.StaticText(self.启动窗口,size=(80, 32),pos=(180, 140),label='水面状态：',name='staticText',style=17)
        标签8_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.标签8.SetFont(标签8_字体)
        self.浊度 = wx.StaticText(self.启动窗口,size=(64, 32),pos=(80, 20),label='0000',name='staticText',style=17)
        浊度_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.浊度.SetFont(浊度_字体)
        self.温度 = wx.StaticText(self.启动窗口,size=(64, 32),pos=(80, 60),label='00.0℃',name='staticText',style=17)
        温度_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.温度.SetFont(温度_字体)
        self.湿度 = wx.StaticText(self.启动窗口,size=(64, 32),pos=(80, 100),label='00%',name='staticText',style=17)
        湿度_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.湿度.SetFont(湿度_字体)
        self.PH = wx.StaticText(self.启动窗口,size=(64, 32),pos=(80, 140),label='00.0',name='staticText',style=17)
        PH_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.PH.SetFont(PH_字体)
        self.高度 = wx.StaticText(self.启动窗口,size=(80, 32),pos=(280, 20),label='0000mm',name='staticText',style=17)
        高度_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.高度.SetFont(高度_字体)
        self.浊度危 = wx.StaticText(self.启动窗口,size=(64, 32),pos=(280, 61),label='正常',name='staticText',style=17)
        浊度危_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.浊度危.SetFont(浊度危_字体)
        self.浊度危.SetForegroundColour((0, 128, 0, 255))
        self.PH危 = wx.StaticText(self.启动窗口,size=(64, 32),pos=(280, 100),label='正常',name='staticText',style=17)
        PH危_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.PH危.SetFont(PH危_字体)
        self.PH危.SetForegroundColour((0, 128, 0, 255))
        self.高度危 = wx.StaticText(self.启动窗口,size=(80, 32),pos=(280, 140),label='正常',name='staticText',style=17)
        高度危_字体 = wx.Font(12,74,90,400,False,'Microsoft YaHei UI',-1)
        self.高度危.SetFont(高度危_字体)
        self.高度危.SetForegroundColour((0, 128, 0, 255))
        self.first_flag=0
        self.step=288

    def 按钮1_按钮被单击(self,event):
        self.高度危.Label = '正常'
        self.高度危.SetForegroundColour((0, 128, 0, 255))
        self.高度危.Refresh()

    def 启动窗口_将被关闭(self,event):
        print('启动窗口,将被关闭')
        wx.Abort() # wx.Exit()

    def 时钟1_周期事件(self,event):
        accessToken = (dict(iot.conectIoT(appID,secret,cert)))['accessToken']
        data = (dict(json.loads(iot.getDeviceMsg(appID,accessToken,deviceID,cert))))['data']['data']
        print(data)
        tu,tp,sd,h,ph=data.replace('TU:','').replace('TP:','').replace('PH:','').replace('H:','').replace('C','').replace('mm','').split()
        self.浊度.Label = tu
        self.温度.Label = tp + '℃'
        self.湿度.Label = sd
        self.高度.Label = h + 'mm'
        self.PH.Label = ph
        # print(tu,tp,sd,h,ph)
        if float(ph)<5.5 or float(ph)>8.5:
            self.PH危.Label = '异常'
            self.PH危.SetForegroundColour((255, 0, 0, 255))
            self.PH危.Refresh()
        else:
            self.PH危.Label = '正常'
            self.PH危.SetForegroundColour((0, 128, 0, 255))
            self.PH危.Refresh()
            
        if int(tu)>200:
            self.浊度危.Label = '异常'
            self.浊度危.SetForegroundColour((255, 0, 0, 255))
            self.浊度危.Refresh()
        else:
            self.浊度危.Label = '正常'
            self.浊度危.SetForegroundColour((0, 128, 0, 255))
            self.浊度危.Refresh()
        
        self.fm=nq.get('http://www.svip886.cn/1.txt').text
        if self.fm=='1':
            self.高度危.Label = '异物入水'
            self.高度危.SetForegroundColour((255, 0, 0, 255))
            self.高度危.Refresh()
        else:
            self.高度危.Label = '正常'
            self.高度危.SetForegroundColour((0, 128, 0, 255))
            self.高度危.Refresh()
        """if self.first_flag==1:
            if self.step!=int(h):
                if abs(int(h)-self.step)>5 and h!=288:
                    self.高度危.Label = '异物入水'
                    self.高度危.SetForegroundColour((255, 0, 0, 255))
                    self.高度危.Refresh()
        """        
        if self.step!=int(h):
            self.first_flag=1
        self.step=int(h)
        
            

class myApp(wx.App):
    def  OnInit(self):
        self.frame = Frame()
        self.frame.Show(True)
        return True

if __name__ == '__main__':
    app = myApp()
    app.MainLoop()